Imports System
Imports System.Text
Imports System.Data
Imports System.Collections.Generic

Imports Framework

<CLSCompliant(True)> _
Public Enum ELanguage
    Null = 0
    English = 1
    'Chinese_Simplified = 2
    'Chinese_Traditional = 3
    Chinese = 2
    Hindi = 6
End Enum


'Table-Row Class (Customisable half)
Partial Public Class CLanguage

#Region "Constants"
#End Region

#Region "Constructors (Public)"
    'Default Connection String
    Public Sub New()
        MyBase.New()
    End Sub
    
    'Alternative Connection String
    Public Sub New(ByVal dataSrc as CDataSrc)
        MyBase.New(dataSrc)
    End Sub
    
    'Hidden (UI code should use cache instead)
    Protected Friend Sub New([languageId] As Byte)
        MyBase.New([languageId])
    End Sub
    Protected Friend Sub New(ByVal dataSrc As CDataSrc, [languageId] As Byte)
        MyBase.New(dataSrc, [languageId])
    End Sub
    Protected Friend Sub New(ByVal dataSrc As CDataSrc, ByVal [languageId] As Byte, ByVal txOrNull As IDbTransaction)
        MyBase.New(dataSrc, [languageId], txOrNull)
    End Sub
#End Region

#Region "Default Values"
    Protected Overrides Sub InitValues_Custom()
        'm_sampleDateCreated = DateTime.Now
        'm_sampleSortOrder   = 0
    End Sub 
#End Region

#Region "Default Connection String"
    Protected Overrides Function DefaultDataSrc() As CDataSrc
        Return CDataSrc.Default
    End Function
#End Region

#Region "Properties - Relationships"    
    'Relationships - Foriegn Keys (e.g parent)

    'Relationships - Collections (e.g. children)

    Public ReadOnly Property [Products] As CProductList
        Get
            Return CProduct.Cache.GetByLanguageId(Me.LanguageId)
        End Get
    End Property
    Protected Function Products_(ByVal tx As IDbTransaction) As CProductList   'Only used for cascade deletes
        With New CProduct(DataSrc)
            Return .SelectByLanguageId(Me.LanguageId, tx)
        End With
    End Function
#End Region

#Region "Properties - Customisation"
    Public ReadOnly Property IsEnglish As Boolean
        Get
            Return ELanguage.English = Me.LanguageId
        End Get
    End Property
    Public ReadOnly Property NativeAndName As String
        Get
            If LanguageId = ELanguage.English Then Return LanguageName
            Return String.Concat(LanguageNative, " ", LanguageName)
        End Get
    End Property
    Public ReadOnly Property NameAndCountActive() As String
        Get
            Return CUtilities.NameAndCount(LanguageName, Products.Active) 'See also LangaugeNative
        End Get
    End Property
    Public ReadOnly Property NativeAndCountActive() As String
        Get
            Return CUtilities.NameAndCount(NativeAndName, Products.Active) 'See also LangaugeNative

        End Get
    End Property
    Public ReadOnly Property NameAndCountActive(typeId As Byte) As String
        Get
            If typeId = 0 Then
                Return CUtilities.NameAndCount(LanguageName, Products.Active)
            Else
                Return CUtilities.NameAndCount(LanguageName, Products.Active.GetByTypeId(typeId))
            End If
        End Get
    End Property
    Public ReadOnly Property NameAndCountActiveSuffixed(typeId As Byte) As String
        Get
            If LanguageId = ELanguage.English Then
                Return NameAndCountActive(typeId)
            Else
                Return String.Concat(NameAndCountActive(typeId), " ", LanguageNative)
            End If
        End Get
    End Property
    Public ReadOnly Property ProductsCount As String 'For sorting
        Get
            Return Products.Count
        End Get
    End Property
#End Region

#Region "Save/Delete Overrides"
    'Can Override MyBase.Save/Delete (e.g. Cascade deletes, or insert related records)
    Public Overrides Sub Delete(ByVal txOrNull As IDbTransaction)
        If Me.LanguageId = ELanguage.English Then Throw New Exception("Cannot delete this language")

        'Use a transaction if none supplied
        If txOrNull Is Nothing Then
            BulkDelete(Me)
            Exit Sub
        End If


        'Cascade-Update
        Dim list As CProductList = Me.Products_(txOrNull)
        For Each i As CProduct In list
            i.ProductLanguageId = ELanguage.English
            i.Save(txOrNull)
        Next

        'Normal Delete
        MyBase.Delete(txOrNull)
    End Sub
#End Region

#Region "Custom Database Queries"
    '(Not normally required for cached classes, use list class for searching etc)
    'For Stored Procs can use: MakeListTyped (matching schema), or DataSrc.ExecuteDataset (reports etc)
    'For Dynamic sql, can use: SelectSum, SelectDistinct, SelectCount, SelectWhere (inherited methods)
#End Region

#Region "Searching (Optional)"
    'For cached classes, custom seach logic resides in static methods on the list class
    ' e.g. CLanguage.Cache.Search("..")

    'See also the auto-generated methods based on indexes
    ' e.g. CLanguage.Cache.GetBy...
#End Region

#Region "Caching Details"
    'Cache Key
    Friend Shared CACHE_KEY As String = GetType(CLanguage).ToString 'TABLE_NAME

    'Cache data
    Private Shared Function LoadCache() As CLanguageList
        Return New CLanguage().SelectAll()
    End Function

    'Cache Timeout
    Private Shared Sub SetCache(ByVal value As CLanguageList)
        If Not IsNothing(value) Then value.Sort()
        CCache.Set(CACHE_KEY, value) 'Optional parameter can override timeout (otherwise uses config-settings, which default to 3hrs)
    End Sub

    'Helper Method
    Private Function CacheGetById(ByVal list As CLanguageList) As CLanguage
        Return list.GetById(Me.LanguageId)
    End Function
#End Region

#Region "Cloning"
    Public Function Clone(ByVal target As CDataSrc, ByVal txOrNull As IDbTransaction) As CLanguage ', parentId As Integer) As CLanguage
        'Shallow copy: Copies the immediate record, excluding autogenerated Pks
        Dim copy As New CLanguage(Me, target)

        'Deep Copy - Child Entities: Cloned children must reference their cloned parent
        'copy.SampleParentId = parentId

        copy.Save(txOrNull)

        'Deep Copy - Parent Entities: Cloned parents also clone their child collections
        'Me.Children.Clone(target, txOrNull, copy.LanguageId)

        Return copy
    End Function
#End Region

#Region "ToXml"
    Protected Overrides Sub ToXml_Custom(ByVal w As System.Xml.XmlWriter)
        'Store(w, "Example", Me.Example)
    End Sub
#End Region

End Class
